package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Scanner;

/**
 * Controller 控制器
 * 该层级的类主要的工作就是与用户打交道
 * 接受用户的请求，后期协调业务类完成业务处理，并将结果回馈给用户
 *
 * UserController
 * 负责处理与"用户"数据相关的请求，比如注册，登录，修改密码等等
 */
@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    @Autowired
    private User user;

    public void start(){
        System.out.println("请输入操作序号:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1:注册");
        System.out.println("2:登录");
        System.out.println("3:修改密码");
        System.out.println("4:用户列表");
        int num = scanner.nextInt();//获取用户输入的序号
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updataUser();
                break;
            case 4:
                listUser();
                break;
            default:
                System.out.println("输入错误");
        }
    }
    /**
     * 用户列表
     */
    private void updataUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("修改密码");
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入旧密码");
        String oldPassword = scanner.nextLine();
        System.out.println("请输入新密码");
        String newPassword = scanner.nextLine();
        //通过用户名查询用户信息
        User user = userMapper.selectbyusernameUser(username);
        if (user != null && user.getPassword().equals(oldPassword)){
                user.setPassword(newPassword);
                int result = userMapper.updateUser(user);
                System.out.println(result > 0 ? "修改成功" : "修改失败");
        }else {
            System.out.println("用户名或密码错误");
        }
    }

    /**
     * 用户列表
     */
    private void listUser(){
        System.out.println("用户列表");
        List<User> list = userMapper.selectAllUser();
        for (User u : list) {
            System.out.println(u);
        }
    }
    /**
     * 处理用户注册
     */
    private void loginUser(){
        System.out.println("欢迎登录");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();

        User user = new User(0,username,password,null,0);

        User result = userMapper.selectbyusernameUser(username);
        if (result != null){
            if (result.getPassword().equals(password)){
                System.out.println("登录成功");
            }else {
                System.out.println("密码错误");
            }
        }else {
            System.out.println("该用户不存在!");
        }
    }


    /**
     * 处理用户注册
     */
    private void regUser(){
        System.out.println("欢迎注册");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.nextLine();
        System.out.println("请输入密码");
        String password = scanner.nextLine();
        System.out.println("请输入昵称");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄");
        int age = scanner.nextInt();
        //必要的验证
        //是否没输入信息，用户名是否重复...

        //用一个User对象表示注册信息
        User user = new User(0,username,password,nickname,age);


        //调用持久层的功能，将用户数据进行保存
        int result = userMapper.insertUser(user);
        //返回结果给用户
        System.out.println(result>0?"注册成功":"注册失败");
    }
}










